
#include <glib/gstdio.h>
#include <gio/gio.h>
#include "account.h"
#include "sqlite3.h"

#define TMP_SOFTWARE_NAME sys_get_process_name()
#define TMP_TCP_SERVER_PORT 4444

static void incoming_cb (GSocketService *service, GSocketConnection *conn,
		GObject *source_object, gpointer user_data);
int main(int argc, char **argv)
{
	char *filename = NULL;
	const char *obj_str = NULL;
	sqlite3 *sql = NULL;
  GObject *object = NULL;
	gint rc = 0, port = TMP_TCP_SERVER_PORT;
	GError *error = NULL;
	GSocketService *service;
	GSettings *settings = NULL;

	g_setenv ("GSETTINGS_SCHEMA_DIR", ".", FALSE);
	g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);

	settings = g_settings_new ("org.gtk.ts");
  object = g_object_new (G_TYPE_OBJECT, NULL);
	obj_str = g_settings_get_string(settings, "db-name");
	DEBUG("sqlname:%s\n", obj_str);// & 0x00ff);
  rc = sqlite3_open(obj_str, &sql);
  if (rc != SQLITE_OK) {
          perror(sqlite3_errmsg(sql));
          goto err;
  }
  g_object_set_data(object, "sql", sql);

	obj_str = g_settings_get_string(settings, "port");
	port = atoi(obj_str);
	service = g_threaded_socket_service_new (-1);
	g_socket_listener_add_inet_port(G_SOCKET_LISTENER(service),
			port, NULL, &error);
	if (error) {
	}
	DEBUG("TCP SERVER:%d\n", port);
	g_signal_connect(service, "run", G_CALLBACK(incoming_cb), object);

	GMainLoop *loop = NULL;
	loop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(loop);
	g_main_loop_unref(loop);
err:
	return rc;
}
static void incoming_cb (GSocketService *service, GSocketConnection *conn,
		GObject *source_object, gpointer user_data)
{
	//GSocketConnection **cconn = user_data;
	GInputStream *istream;
	GOutputStream *ostream;
	GSocketAddress *sockaddr = NULL;
	GInetAddress *inetaddr;
	guint8 buffer[1024] = {0};//*buffer = NULL;
	gchar *addr;//*buffer = NULL;
	gint port = 0, buffer_size = 0, rc = 0;
	GError *error = NULL;
	void *sql = NULL, *object = NULL;
  char *data = NULL;
  size_t len = 0;
	//char buf[128] = {0}, pointer[128] = {0};
	//const char *obj_str = NULL;
	//json_object *obj = NULL;

  object = user_data;
	sql = g_object_get_data(object, "sql");
	buffer_size = sizeof(buffer) - 1;//1024;
	/*
	buffer = g_new0 (guint8, buffer_size);
	client = matter_calloc();
	matter_set_pointer(client, "conn", conn, MATTER_SET_CREAT);
	matter_set_string(client, "buffer", buffer, MATTER_SET_CREAT);
	matter_set_int2string(client, "buffer_size", buffer_size,
			MATTER_SET_CREAT);
			*/
	sockaddr = g_socket_connection_get_remote_address(conn, &error);
	g_object_get (sockaddr, "address", &inetaddr, "port", &port, NULL);
	addr = g_inet_address_to_string (inetaddr);
	g_debug("CLIENT:%s[%d]conn:%p\n", addr, port, conn);
	g_free (addr);

	istream = g_io_stream_get_input_stream (G_IO_STREAM (conn));
	ostream = g_io_stream_get_output_stream (G_IO_STREAM (conn));
	while (1) {
		rc = g_input_stream_read (istream, buffer, buffer_size,
				NULL, &error);
		if (rc) {
			buffer[rc] = '\0';
			DEBUG("++++:\n%s\n", buffer);
			len = account_handle_msg(conn, buffer, rc, sql, &data);
      if (data) {
        rc = g_output_stream_write(ostream, data, len, NULL, &error);
        if (rc == len) {
          DEBUG("----:%s\n", data);
        } else {
          DEBUG_ERR("%s\n", error->message);
          goto err;
        }
        g_free(data);
      }
		}
		if (error) {
			DEBUG_ERR("%s\n", error->message);
			goto err;
		}
		g_usleep(100);
	}
err:
	return;
}
